package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.entity.HouseHoldinfos;
import com.woniu.entity.Households;
import com.woniu.mapper.HouseHoldinfosMapper;
import com.woniu.mapper.HouseholdsMapper;
import com.woniu.service.IHouseholdsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Gapper
 * @since 2021-03-29
 */
@Service
public class HouseholdsServiceImpl extends ServiceImpl<HouseholdsMapper, Households> implements IHouseholdsService {

    @Autowired
    private HouseholdsMapper householdsMapper;
    @Autowired
    private HouseHoldinfosMapper houseHoldinfosMapper;

    /**
     查找全部
     */
    @Override
    public List<Households> findAll() {
        List<Households> list = householdsMapper.find(new QueryWrapper<Households>());
        return list;
    }

    /**
     根据id查一个
     */
    @Override
    public List<Households> findById(String hid) {
        QueryWrapper queryWrapper=new QueryWrapper<Households>();
        queryWrapper.in("HouseHold_ID",hid);
        List list = householdsMapper.find(queryWrapper);
        return list;
    }

    /**
     删除房屋信息的同时，删除房屋详情信息
     */
    @Override
    @Transactional
    public int deleteById(String hid) {
        //hfid
        QueryWrapper fquery=new QueryWrapper();
        fquery.in("HouseHold_ID",hid);
        List<HouseHoldinfos>hflist  = houseHoldinfosMapper.selectList(fquery);
        //将hflist存入集合
        Set hfid=new HashSet();
        for(int i=0;i<hflist.size(); i++){
            hfid.add(hflist.get(i).getHouseholdinfoId());
        }
        int rows=0;
        //删除房屋详情
        if(hfid.size() > 0){
            rows=houseHoldinfosMapper.deleteBatchIds(hfid);
        }
        rows= householdsMapper.deleteBatchIds(Arrays.asList(hid));

        return rows;
    }
}
